
package com.hongchen.model;

import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.Generator;
import com.jfinal.plugin.activerecord.generator.ModelGenerator;
import com.jfinal.plugin.druid.DruidPlugin;
import com.realdtech.base.ModelSupport;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * Model、BaseModel、_MappingKit 生成器
 */
public class _Generator {
    public static final String BASE_MODEL_PACKAGE_NAME = "com.hongchen.model.base";
    public static final String MODEL_PACKAGE_NAME = "com.hongchen.model";
    public static final String BASE_MODEL_OUTPUT_DIR = PathKit.getWebRootPath()
            + "\\src\\main\\java\\com\\hongchen\\model\\base";

    public static final String MODEL_OUTPUT_DIR = BASE_MODEL_OUTPUT_DIR + "/..";
    public static final String[] excludedTable = {"sys_config","sys_dict", "sys_log", "sys_resource", "sys_role", "sys_role_resource", "sys_user", "sys_user_role", "sys_site", "sys_user_site", "sys_org", "sys_user_org"};

    /**
     * 重用 JFinalClubConfig 中的数据源配置，避免冗余配置
     */
    public static DataSource getDataSource() {
        DruidPlugin druidPlugin = new DruidPlugin("jdbc:mysql://realgold.mysql.rds.aliyuncs.com/xws_crm_test", "pb_test", "!qa2ws3ed");
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }

    public static void main(String[] args) {

        System.out.println("：" + BASE_MODEL_OUTPUT_DIR);

        // 创建生成器
        // Generator gen = new Generator(getDataSource(),
        // BASE_MODEL_PACKAGE_NAME, BASE_MODEL_OUTPUT_DIR,
        // MODEL_PACKAGE_NAME, MODEL_OUTPUT_DIR);
        Generator gen = new Generator(getDataSource(),
                new _BaseModelGenerate(BASE_MODEL_PACKAGE_NAME, BASE_MODEL_OUTPUT_DIR, ModelSupport.class),
                new ModelGenerator(MODEL_PACKAGE_NAME, BASE_MODEL_PACKAGE_NAME, MODEL_OUTPUT_DIR));
        // 设置数据库方言
        gen.setDialect(new MysqlDialect());

        // 添加不需要生成的表名
        for (String table : excludedTable) {
            gen.addExcludedTable(table);
        }
        // 添加不需要生成的表名
        gen.addExcludedTable(getExcTab("wf_"));
        // 设置是否在 Model 中生成 dao 对象
        gen.setGenerateDaoInModel(false);
        // 设置是否生成字典文件
        gen.setGenerateDataDictionary(true);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为
        // "User"而非 OscUser
        // gernerator.setRemovedTableNamePrefixes("t_");
        // 生成
        gen.generate();
    }
    private static String[] getExcTab(String preName){
        String sql="SELECT table_name from information_schema.tables WHERE table_name LIKE '"+preName+"%'";
        List<String> list = new ArrayList<String>();
        Connection conn = null;
        try {
            conn = getDataSource().getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs=stmt.executeQuery(sql);
            while (rs.next()) {
                list.add(rs.getString(1));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String[] s=new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            s[i]= list.get(i);
        }
        return s;
    }
}
